package com.dartfar.mail.action;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.dartfar.base.action.BaseAction;
import com.dartfar.mail.model.Mail;
import com.dartfar.mail.model.MailState;
import com.dartfar.mail.model.SendState;
import com.dartfar.mail.service.MailService;
import com.dartfar.mail.vo.MailVo;

/**
 * 邮件Action对象
 * @author 王琦
 */
@Component("mailAction")
@Scope("prototype")
public class MailAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * Service对象
	 */
	@Resource
	private MailService mailService;
	
	
	/**
	 * 页面传参
	 */
	private String mailTitle;	//邮件标题
	private String receiveIds;	//收件人
	private String mailContent;	//邮件内容
	private String mailAtts;	//附件
	
	private File[] upload;
	private String[] uploadFileName;
	private File file;
	
	/**
	 * 页面跳转
	 * 
	 * 收件夹列表
	 */
	public String receive_List(){
		return "receive_List";
	}
	
	/**
	 * 未读邮件列表
	 */
	public String unRead_List(){
		return "unRead_List";
	}
	
	/**
	 * 已发送邮件列表
	 */
	public String send_List(){
		return "send_List";
	}
	
	/**
	 * 写邮件
	 */
	public String write(){
		return "write";
	}
	
	
	/**
	 * 发送邮件
	 */
	public String sendMail(){
		if (curUser() != null) {
			Mail mail = new Mail();
			mail.setTitle(mailTitle);
			mail.setContent(mailContent);
			mail.setDate(new Date());
			try{
				mailService.sendMail(curUser().getId(), receiveIds.split(","), mail);
			}catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			
			
			System.out.println("mailAtts: " + mailAtts);
			String[] strs = mailAtts.split("|");
			
			for(int i = 0; i < strs.length; i++){
				System.out.println("mailAtts: " + strs[i]);
			}
			
			//upload = 
			
			if(upload!=null&&upload.length>0){
				mailService.upload(mail, upload, null, uploadFileName);
				//mailManager.upload(mail, upload, caption, uploadFileName);
			}
			
		}
		
		
		
		
/*		if(upload!=null&&upload.length>0){
			mailManager.upload(mail, upload, caption, uploadFileName);
		}*/
		return null;
	}
	
	/**
	 * 已发送邮件
	 */
	public String send(){
		List<MailVo> mailVos = new ArrayList<MailVo>();
		MailVo mo = null;
		if (curUser() != null){
			mailVos.clear();
			List<SendState> list = mailService.getSendListByUserId(curUser().getId());
			for(Iterator<SendState> i =  list.iterator();i.hasNext();){
				SendState s = i.next();
				mo = new MailVo();
				Mail mail = s.getMail();
				mo.setId(s.getId());
				mo.setReceiveNames(s.getReceiveNames());
				mo.setTitle(mail.getTitle());
				mo.setDate(sdf.format(s.getSendDate()));
				mailVos.add(mo);
			}
		}
		
		String jsonResult = JSONArray.fromObject(mailVos).toString();
		try{
			response.getWriter().write(jsonResult);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 收件夹
	 */
	public String receive(){
		List<MailVo> mailVos = new ArrayList<MailVo>();
		if (curUser() != null){
			List<MailState> list = mailService.getReceiveListByUserId(curUser().getId());
			for(Iterator<MailState> i =  list.iterator();i.hasNext();){
				MailState m = i.next();
				MailVo mo = new MailVo();
				Mail mail = m.getMail();
				mo.setId(m.getId());
				if(m.getSendUser() == null){
					mo.setSendName("系统邮件");
				}else{
					mo.setSendName(m.getSendUser().getPerson().getName());
				}
				mo.setReceiveNames(m.getReceiveUser().getPerson().getName());
				mo.setTitle(mail.getTitle());
				mo.setDate(sdf.format(m.getReceiveDate()));
				mo.setContent(mail.getContent());
				mailVos.add(mo);
			}
		}
		String jsonResult = JSONArray.fromObject(mailVos).toString();
		try{
			response.getWriter().write(jsonResult);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * 未读邮件
	 */
	public String unRead(){
		List<MailVo> mailVos = new ArrayList<MailVo>();
		MailVo mo = null;
		mailVos.clear();
		List<MailState> list = mailService.receiveMail(curUser().getId());
		for(Iterator<MailState> i =  list.iterator();i.hasNext();){
			MailState m = i.next();
			mo = new MailVo();
			Mail mail = m.getMail();
			mo.setId(m.getId());
			if(m.getSendUser() == null){
				mo.setSendName("系统邮件");
			}else{
				mo.setSendName(m.getSendUser().getPerson().getName());
			}
			mo.setTitle(mail.getTitle());
			mo.setDate(sdf.format(m.getReceiveDate()));
		}
		String jsonResult = JSONArray.fromObject(mailVos).toString();
		try{
			response.getWriter().write(jsonResult);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 *  Gets And Sets
	 */
	public String getReceiveIds() {
		return receiveIds;
	}

	public String getMailTitle() {
		return mailTitle;
	}

	public void setMailTitle(String mailTitle) {
		this.mailTitle = mailTitle;
	}

	public void setReceiveIds(String receiveIds) {
		this.receiveIds = receiveIds;
	}

	public String getMailContent() {
		return mailContent;
	}

	public void setMailContent(String mailContent) {
		this.mailContent = mailContent;
	}

	public String getMailAtts() {
		return mailAtts;
	}

	public void setMailAtts(String mailAtts) {
		this.mailAtts = mailAtts;
	}



	
	
	
}
